#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

#define MAX_USERS 100
#define MAX_USERNAME_LENGTH 50
#define MAX_PASSWORD_LENGTH 50

typedef struct {
    char username[MAX_USERNAME_LENGTH];
    char password[MAX_PASSWORD_LENGTH];
} User;

User users[MAX_USERS];
int userCount = 0;

bool registerUser() {
    printf("\n--- 注册 ---\n");
    
    char username[MAX_USERNAME_LENGTH];
    printf("请输入用户名: ");
    scanf("%s", username);
    
    // 检查用户名是否已存在
    for (int i = 0; i < userCount; i++) {
        if (strcmp(users[i].username, username) == 0) {
            printf("错误: 用户名已存在!\n");
            return false;
        }
    }
    
    char password[MAX_PASSWORD_LENGTH];
    int hasUpper = 0, hasLower = 0, hasDigit = 0;
    int isValid = 0;
    while (!isValid) {
        printf("请输入密码（必须包含大小写字母和数字）: ");
        if (scanf("%99s", password) != 1) { 
            printf("输入错误，请重新输入。\n");
            while (getchar() != '\n');    
            continue;
        }

                                // 检查密码复杂度
        for (int i = 0; password[i] != '\0'; i++) {
            if (isupper((unsigned char)password[i])) hasUpper = 1;
            if (islower((unsigned char)password[i])) hasLower = 1;
            if (isdigit((unsigned char)password[i])) hasDigit = 1;
        }

        isValid = hasUpper && hasLower && hasDigit;

        if (!isValid) {
            printf("密码不符合要求，请确保包含大小写字母和数字。\n");
            hasUpper = hasLower = hasDigit = 0;
        }
    }
    printf("密码设置成功！\n");
    
    // 添加新用户
    if (userCount < MAX_USERS) {
        strcpy(users[userCount].username, username);
        strcpy(users[userCount].password, password);
        userCount++;
        printf("注册成功，欢迎 %s!\n", username);
        return true;
    } else {
        printf("错误: 用户数量已达到上限!\n");
        return false;
    }
}

bool loginUser() {
    printf("\n--- 登录 ---\n");
    
    char username[MAX_USERNAME_LENGTH];
    printf("请输入用户名: ");
    scanf("%s", username);
    
    char password[MAX_PASSWORD_LENGTH];
    

    // 验证用户
    for (int i = 0; i < userCount; i++) {
        if (strcmp(users[i].username, username) == 0 && 
            strcmp(users[i].password, password) == 0) {
            printf("登录成功，欢迎回来 %s!\n", username);
            return true;
        }
    }
    
    printf("错误: 用户名或密码不正确!\n");
    return false;
}

int main() {
    int choice;
    
    do {
        printf("\n===== 用户管理系统 =====\n");
        printf("1. 注册\n");
        printf("2. 登录\n");
        printf("3. 退出\n");
        printf("请选择操作 (1-3): ");
        
        // 验证输入是否为有效整数
        if (scanf("%d", &choice) != 1) {
            printf("错误: 请输入数字!\n");
            // 清除输入缓冲区
            while (getchar() != '\n');
            choice = 0; // 设置为无效值，触发错误处理
            continue;
        }
        
        // 清除输入缓冲区中的多余字符
        while (getchar() != '\n');
        
        switch (choice) {
            case 1:
                registerUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                printf("感谢使用，再见!\n");
                break;
            default:
                printf("错误: 无效选择，请输入1-3之间的数字!\n");
        }
    } while (choice != 3);
    
    return 0;
}